package com.sugar.subscribe.course.module.business.controller.admin.coachwallet;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.pojo.CommonResult;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;
import static com.sugar.subscribe.course.framework.common.pojo.CommonResult.success;

import com.sugar.subscribe.course.framework.excel.core.util.ExcelUtils;

import com.sugar.subscribe.course.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.subscribe.course.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.subscribe.course.module.business.controller.admin.coachwallet.vo.*;
import com.sugar.subscribe.course.module.business.dal.dataobject.coachwallet.CoachWalletDO;
import com.sugar.subscribe.course.module.business.service.coachwallet.CoachWalletService;

@Tag(name = "管理后台 - 教练钱包")
@RestController
@RequestMapping("/business/coach-wallet")
@Validated
public class CoachWalletController {

    @Resource
    private CoachWalletService coachWalletService;

    @DeleteMapping("/delete")
    @Operation(summary = "删除教练钱包")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:coach-wallet:delete')")
    public CommonResult<Boolean> deleteCoachWallet(@RequestParam("id") Long id) {
        coachWalletService.deleteCoachWallet(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除教练钱包")
                @PreAuthorize("@ss.hasPermission('business:coach-wallet:delete')")
    public CommonResult<Boolean> deleteCoachWalletList(@RequestParam("ids") List<Long> ids) {
        coachWalletService.deleteCoachWalletListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得教练钱包")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:coach-wallet:query')")
    public CommonResult<CoachWalletRespVO> getCoachWallet(@RequestParam("id") Long id) {
        CoachWalletDO coachWallet = coachWalletService.getCoachWallet(id);
        return success(BeanUtils.toBean(coachWallet, CoachWalletRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得教练钱包分页")
    @PreAuthorize("@ss.hasPermission('business:coach-wallet:query')")
    public CommonResult<PageResult<CoachWalletRespVO>> getCoachWalletPage(@Valid CoachWalletPageReqVO pageReqVO) {
        PageResult<CoachWalletDO> pageResult = coachWalletService.getCoachWalletPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CoachWalletRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出教练钱包 Excel")
    @PreAuthorize("@ss.hasPermission('business:coach-wallet:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCoachWalletExcel(@Valid CoachWalletPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CoachWalletDO> list = coachWalletService.getCoachWalletPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "教练钱包.xls", "数据", CoachWalletRespVO.class,
                        BeanUtils.toBean(list, CoachWalletRespVO.class));
    }

}